<!DOCTYPE html>
<html lang="en">
  <head>
    <% if user_signed_in? && current_page?(root_path) %>
      <% if params[:feed_type] == "discover" || params[:feed_type].blank? %>
        <link rel="preload" href="/stories/feed/?page=1&type_of=discover" as="fetch" crossorigin="same-origin">
      <% elsif params[:feed_type] == "following" %>
        <link rel="preload" href="/stories/feed/?page=1&type_of=following" as="fetch" crossorigin="same-origin">
      <% end %>
      <link rel="preload" href="/<%= ENV.fetch("BILLBOARD_URL_COMPONENT", "bb") %>/feed_first" as="fetch" crossorigin="same-origin">
      <link rel="preload" href="/<%= ENV.fetch("BILLBOARD_URL_COMPONENT", "bb") %>/feed_second" as="fetch" crossorigin="same-origin">
      <link rel="preload" href="/<%= ENV.fetch("BILLBOARD_URL_COMPONENT", "bb") %>/feed_third" as="fetch" crossorigin="same-origin">
      <link rel="preload" href="/sidebars/home" as="fetch" crossorigin="same-origin">
    <% end %>
    <meta charset="utf-8">
    <% title = yield(:title) %>
    <title><%= title || community_name.to_s %></title>
    <%= yield(:page_meta) %>
    <% unless internal_navigation? %>
      <meta name="last-updated" content="<%= Time.current %>">
      <meta name="user-signed-in" content="<%= user_signed_in? %>">
      <meta name="head-cached-at" content="<%= Time.current.to_i %>">
      <meta name="environment" content="<%= Rails.env %>">
      <%= render "layouts/styles", qualifier: "main" %>
      <%= javascript_include_tag "base", "application", "baseInitializers", "baseTracking", "followButtons", defer: true %>

      <%# This Rails.env.test? introduces some test-specific behavior, but that is much easier to verify without the specific loading code. We should ensure search still works as expected manually as well. %>
      <% if controller_name == "articles_search" || Rails.env.test? %>
        <%= javascript_include_tag "Search", defer: true %>
      <% else %>
        <meta name="search-script" content="<%= asset_path 'Search.js' %>">
      <% end %>
      <% if Settings::UserExperience.show_mobile_app_banner %>
        <%= javascript_include_tag "runtimeBanner", defer: true %>
      <% end %>
      <% if user_signed_in? %>
        <%= javascript_include_tag "onboardingRedirectCheck", "contentDisplayPolicy", defer: true %>
      <% end %>
      <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
      <%= favicon_link_tag Images::Optimizer.call(Settings::General.favicon_url, width: 32) %>
      <link rel="apple-touch-icon" href="<%= optimized_image_url(Settings::General.logo_png, width: 180, fetch_format: "png") %>">
      <link rel="apple-touch-icon" sizes="152x152" href="<%= optimized_image_url(Settings::General.logo_png, width: 152, fetch_format: "png") %>">
      <link rel="apple-touch-icon" sizes="180x180" href="<%= optimized_image_url(Settings::General.logo_png, width: 180, fetch_format: "png") %>">
      <link rel="apple-touch-icon" sizes="167x167" href="<%= optimized_image_url(Settings::General.logo_png, width: 167, fetch_format: "png") %>">
      <link href="<%= optimized_image_url(Settings::General.logo_png, width: 192, fetch_format: "png") %>" rel="icon" sizes="192x192" />
      <link href="<%= optimized_image_url(Settings::General.logo_png, width: 128, fetch_format: "png") %>" rel="icon" sizes="128x128" />
      <meta name="apple-mobile-web-app-title" content="<%= Settings::General.app_domain %>">
      <meta name="application-name" content="<%= Settings::General.app_domain %>">
      <meta name="theme-color" content="#ffffff" media="(prefers-color-scheme: light)">
      <meta name="theme-color" content="#000000" media="(prefers-color-scheme: dark)">
      <link rel="search" href="<%= URL.url("open-search.xml") %>" type="application/opensearchdescription+xml" title="<%= community_name %>" />

      <meta property="forem:name" content="<%= community_name %>" />
      <meta property="forem:logo" content="<%= optimized_image_url(Settings::General.logo_png, width: 512, fetch_format: "png") %>" />
      <meta property="forem:domain" content="<%= Settings::General.app_domain %>" />
    <% end %>
    <%= Settings::UserExperience.head_content.html_safe %>
  </head>
  <% unless internal_navigation? %>
    <% cache(release_adjusted_cache_key("top-html-and-config--#{user_signed_in?}--#{@article.present?}-#{request.host}-#{I18n.locale}-#{@article&.subforem_id}")) do %>
      <body
        class="<%= Settings::UserExperience.default_font.tr("_", "-") %>-article-body default-header"
        data-user-status="<%= j(user_logged_in_status) %>"
        data-is-root-subforem="<%= j(is_root_subforem?) %>"
        <% if RequestStore.store[:subforem_id].present? %>
          data-subforem-id="<%= j(RequestStore.store[:subforem_id]) %>"
        <% end %>
        data-side-nav-visible="<%= Subforem.cached_discoverable_ids.size > 1 %>"
        data-community-name="<%= j(community_name) %>"
        data-subscription-icon="<%= asset_path("subscription-icon.png") %>"
        data-locale="<%= j(I18n.locale) %>"
        data-honeybadger-key="<%= j(ApplicationConfig["HONEYBADGER_JS_API_KEY"]) %>"
        data-deployed-at="<%= j(ForemInstance.deployed_at) %>"
        data-latest-commit-id="<%= j(ForemInstance.latest_commit_id) %>"
        data-ga-tracking="<%= j(Settings::General.ga_tracking_id) %>"
        data-cookie-banner-user-context="<%= j(Settings::General.cookie_banner_user_context) %>"
        data-cookie-banner-platform-context="<%= j(Settings::General.coolie_banner_platform_context) %>"
        data-algolia-id="<%= j(Settings::General.algolia_application_id) if feature_flag_enabled?(:algolia_frontend) %>"
        data-algolia-search-key="<%= j(Settings::General.algolia_search_only_api_key) if feature_flag_enabled?(:algolia_frontend) %>"
        data-algolia-display="<%= j(Settings::General.display_algolia_branding) %>"
        data-dynamic-url-component="<%= ENV.fetch("BILLBOARD_URL_COMPONENT", "bb") %>"
        data-ga4-tracking-id="<%= j(Settings::General.ga_analytics_4_id) %>">
        <%# Repeat of stylesheets in <head> to fix rendering glitch: https://github.com/forem/forem/issues/12377 %>
        
        <%# First script to run no matter what %>
        <script>
          if (navigator.userAgent === 'ForemWebView/1' || window.frameElement) {
            document.body.classList.add("hidden-shell");
          }
        </script>

        <%= render "layouts/styles", qualifier: "secondary" %>
        <div id="body-styles">
          <style>
            :root {
              --accent-brand-lighter-rgb: <%= Color::CompareHex.new([Settings::UserExperience.primary_brand_color_hex]).brightness(1.35, only_values: true) %>;
              --accent-brand-rgb: <%= Color::CompareHex.new([Settings::UserExperience.primary_brand_color_hex]).brightness(1, only_values: true) %>;
              --accent-brand-darker-rgb: <%= Color::CompareHex.new([Settings::UserExperience.primary_brand_color_hex]).brightness(0.8, only_values: true) %>;
            }
          </style>
        </div>
        <% if user_signed_in? %>
          <%= render "layouts/user_config" %>
        <% end %>
        <div id="audiocontent" data-podcast="">
          <%= yield(:audio) %>
        </div>
        <% cache("main-side-bar-#{user_signed_in?}", expires_in: 10.minutes) do %>
          <% if Subforem.cached_discoverable_ids.any? %>
            <%= render "layouts/main_side_bar" %>
          <% end %>
        <% end %>
        <%= render "layouts/top_bar" %>
        <div id="active-broadcast" class="broadcast-wrapper"></div>
    <% end %>
  <% end %>
<div id="page-content" class="wrapper <%= view_class %>" data-current-page="<%= current_page %>">
  <% if flash[:global_notice] %>
    <div class="crayons-banner" id="setup-banner" aria-live="polite">
      <%== flash[:global_notice] %>
    </div>
  <% end %>
  <div id="page-content-inner" data-internal-nav="<%= internal_navigation? %>">
    <div id="page-route-change" class="screen-reader-only" aria-live="polite" aria-atomic="true"></div>

    <% if @hero_html && @hero_billboard.blank? %>
      <aside id="hero-html-wrapper" aria-label="Campaign banner" class="overflow-x-hidden" data-name="<%= @hero_area.name %>">
        <script>
          // Make sure html variant element has id hero-html-wrapper and data-name as its name
          if (localStorage.getItem('exited_hero') === '<%= @hero_area.name.html_safe %>') {
            document.getElementById('hero-html-wrapper').style.display = 'none';
          }
        </script>
        <%= @hero_html.html_safe %>
      </aside>
    <% end %>
    <%= yield %>
    <div id="runtime-banner-container"></div>
    <div id="i18n-translations" data-translations="<%= i18n_translations_for_javascript.to_json %>"></div>
  </div>
</div>
<% unless internal_navigation? %>
  <% cache("footer-and-signup-modal-#{user_signed_in?}-#{ForemInstance.deployed_at}-#{Settings::General.admin_action_taken_at&.rfc3339}-#{request.host}", expires_in: 24.hours) do %>
    <%= render "layouts/footer" %>
    <%= render "layouts/signup_modal" unless user_signed_in? %>
    <div id="cookie-consent"></div>
  <% end %>
  <%= render "articles/reaction_category_resources" %>
  <% if RequestStore.store[:subforem_id].present? %>
    <%= render "layouts/bottom_scripts"  %>
  <% end %>
  <%= Settings::UserExperience.bottom_of_body_content.html_safe %>
  
  <% if user_signed_in? && ArticlePolicy.is_root_subforem? %>
    <%= render "shared/subforem_selection_modal" %>
  <% end %>
  
  </body>
  </html>
<% end %>
